#!/bin/sh

# $FreeBSD: ports/x11/slim/files/slim.in,v 1.1 2012/08/05 00:31:46 dougb Exp $
#
# PROVIDE: slim
# REQUIRE: LOGIN dbus hald autoconf
# KEYWORD: shutdown
#
# Add the following lines to /etc/rc.conf to enable slim:
# slim_enable (bool):      Set to "NO" by default.
#                          Set it to "YES" to enable slim
#
# Alternatively, edit /etc/ttys and change the line below
#   ttyv8   "/usr/X11R6/bin/xdm -nodaemon"  xterm   off secure
# to this:
#   ttyv8   "/usr/local/bin/slim"   xterm   on secure

. /etc/rc.subr

name="slim"
rcvar=slim_enable

start_precmd=${name}_rmfile
stop_precmd=${name}_prestop
stop_postcmd=${name}_rmfile

load_rc_config $name

: ${slim_enable="NO"}

command=/usr/local/bin/slim
command_args="-d"

find_pidfile()
{
	if type get_pidfile_from_conf >/dev/null 2>&1 &&
	    get_pidfile_from_conf lockfile /usr/local/etc/${name}.conf; then
		pidfile="$_pidfile_from_conf"
	else
		pidfile="/var/run/${name}.pid"
	fi
}

slim_rmfile ()
{
	local file

	[ -z "$pidfile" ] && find_pidfile

	for file in $pidfile /var/run/slim.auth; do
		[ -e "$file" ] && unlink $file
	done
	
	# Needed if neither file exists
	return 0
}

slim_prestop ()
{
	local xpid

	find_pidfile

	xpid=`ps -axww | grep '/bin/[X] .* -auth /var/run/slim.auth'`
	xpid="${xpid## }"
	[ -n "$xpid" ] && kill ${xpid%% *}
}

run_rc_command "$1"
